package com.pearl.security.multiloginreject.demo.session.redis;

import cn.hutool.core.util.StrUtil;
import com.pearl.security.multiloginreject.demo.response.ResponseUtils;
import com.pearl.security.multiloginreject.demo.util.LoginUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.AccountExpiredException;
import org.springframework.security.core.session.SessionInformation;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.security.web.session.SessionInformationExpiredEvent;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;

import java.io.IOException;

/**
 * @author TD
 * @version 1.0
 * @date 2024/5/14
 */
public class JsonSessionInformationExpiredStrategy implements SessionInformationExpiredStrategy {

    public void onExpiredSessionDetected(SessionInformationExpiredEvent event) throws IOException, ServletException {
        // 失败处理返回JSON
        ResponseUtils.buildResponse(event.getResponse(),StrUtil.format("当前用户已在其他设备登录"), HttpStatus.UNAUTHORIZED);
    }
}
